package com.huchengrui.guesssong2

import android.Manifest
import android.app.AlertDialog
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.CheckBox
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import com.huchengrui.guesssong2.SetPicture.setPicture
import com.huchengrui.guesssong2.databinding.ActivityMainBinding
import java.io.BufferedReader
import java.io.InputStreamReader
import java.net.URL


class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    private var path: String? = null
    private var imageUrl: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        path = getSharedPreferences("data", Context.MODE_PRIVATE).getString(
            "path",
            "/storage/emulated/0/netease/cloudmusic/Music"
        )
        imageUrl = getSharedPreferences("data", Context.MODE_PRIVATE).getString(
            "imageUrl",
            "https://api.lazy.ink/img-phone/"
        )
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
//        setPicture(binding.imageView, URL(imageUrl),this)
        binding.imageView.setImageBitmap(BitmapFactory.decodeFile("${this.externalCacheDir!!.absolutePath}/background.jpg"))
        ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), 1)
        binding.startGuess.setOnClickListener {
            val intent = Intent(this, Guessing::class.java)
            intent.putExtra("path", path)
            startActivity(intent)
        }
        binding.editPath.setOnClickListener {
            showPathEditor()
        }
    }

    private fun showPathEditor() {
        val view = View.inflate(this, R.layout.alert_dialog, null)
        val pathText = view.findViewById<EditText>(R.id.songPath).apply { setText(path) }
        val urlText = view.findViewById<EditText>(R.id.imageUrl).apply { setText(imageUrl) }
        val checkBox = view.findViewById<CheckBox>(R.id.checkBox)
        val button = view.findViewById<Button>(R.id.randomBackground)
        val dialog: AlertDialog = AlertDialog.Builder(this).run {
            setTitle("Settings")
            setView(view)
            setPositiveButton("Confirm") { _, _ ->
                val editor = getSharedPreferences("data", Context.MODE_PRIVATE).edit()
                path = pathText.text.toString()
                imageUrl = urlText.text.toString()
                editor.putString("path", path)
                editor.putString("imageUrl", imageUrl)
                editor.apply()
            }
            create()
        }
        checkBox.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) {
                view.findViewById<TextView>(R.id.imageUrlHint).visibility = View.INVISIBLE
                urlText.visibility = View.INVISIBLE
            } else {
                view.findViewById<TextView>(R.id.imageUrlHint).visibility = View.VISIBLE
                urlText.visibility = View.VISIBLE
            }
        }
        button.setOnClickListener {
            val editor = getSharedPreferences("data", Context.MODE_PRIVATE).edit()
            path = pathText.text.toString()
            imageUrl = urlText.text.toString()
            editor.putString("path", path)
            if (!checkBox.isChecked) {
                editor.putString("imageUrl", imageUrl)
            }
            editor.apply()
            if (checkBox.isChecked) {
                val pictureList = mutableListOf<String>()
                val reader = BufferedReader(InputStreamReader(resources.openRawResource(R.raw.img)))
                reader.use {
                    reader.forEachLine {
                        pictureList.add(it)
                    }
                }
                setPicture(URL(pictureList.random()),this, binding.imageView)
            } else
                setPicture(URL(imageUrl), this@MainActivity, binding.imageView)
        }
        dialog.show()
    }
}